/*
* Copyright (c) 2006, John D'Emic <john.demic@gmail.com>
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the University of California, Berkeley nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.badcurrent.ldapodm.reflection;

import net.badcurrent.ldapodm.ReflectionException;
import net.badcurrent.ldapodm.annotations.LdapAttribute;

import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import org.apache.commons.lang.StringUtils;

/**
 * Reflection helper utilities.
 *
 * @author John D'Emic <john.demic@gmail.com>
 */
public class ReflectionUtils {

    /**
     * Given a JavaBean method, return the property name.
     * @param method
     * @return the property name of a getter or setter.
     */
    public static String getProperty(Method method) {
        if (method.getName().length() < 4) {
            throw new ReflectionException("Cannot extract property from methodName: " + method.getName() +
                    ", methodName is either null or not a JavaBean getter/setter");
        }
        return StringUtils.uncapitalize(method.getName().substring(3));
    }

    public static LdapAttribute findAttributeForProperty(Class clazz, String propertyName) {
       
        for (Field field : clazz.getDeclaredFields()) {
            if (field.getName().equals(propertyName)) {
                if (field.getAnnotation(LdapAttribute.class) != null)
                    return field.getAnnotation(LdapAttribute.class);
            }
        }
        Pattern pattern = Pattern.compile("^(get|set)" + StringUtils.capitalize(propertyName) + "$");

        for (Method method : clazz.getMethods()) {
            Matcher matcher = pattern.matcher(method.getName());
            if (matcher.find(0)) {
                if (method.getAnnotation(LdapAttribute.class) != null)
                    return method.getAnnotation(LdapAttribute.class);
            }
        }
        return null;
    }

}
